package com.chwn.curiousdiary.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.bean.StoryListBean;
import com.chwn.curiousdiary.download.DownHelper;
import com.chwn.curiousdiary.utils.ImgLoadUtil;
import com.chwn.harulib.adapter.ViewHolder;

import java.util.List;

/**
 * Created by Myshikin on 2017/4/11.
 * desc:故事集
 */
public class StoryListAdapter extends AStoryAdapter<StoryListBean> {

    private int errorValue;

    public void setNeedShowTag(boolean needShowTag) {
        this.needShowTag = needShowTag;
    }

    private boolean needShowTag ;

    public StoryListAdapter(Context context, List<StoryListBean> datas, boolean isOpenLoadMore) {
        super(context, datas, isOpenLoadMore);
    }



    @Override
    protected void convert(final ViewHolder holder, final StoryListBean data) {
        final int position = mDatas.indexOf(data);
        errorValue = holder.getAdapterPosition() - position;
        ImageView mIVStory = holder.getView(R.id.iv_story_img);

        String storyTags = data.getStoryTags();
        if (TextUtils.isEmpty(storyTags) || !needShowTag) {
            holder.getView(R.id.ll_tags).setVisibility(View.GONE);
        } else {
            String[] splitTags = storyTags.split(",");
            holder.getView(R.id.ll_tags).setVisibility(View.VISIBLE);
            if (splitTags.length == 1) {
                holder.setText(R.id.tv_one, splitTags[0]);
                holder.setViewVisibility(R.id.tv_one);
                holder.setViewGone(R.id.tv_three);
                holder.setViewGone(R.id.tv_two);
            } else if (splitTags.length == 2) {
                holder.setText(R.id.tv_one, splitTags[0]);
                holder.setText(R.id.tv_two, splitTags[1]);
                holder.setViewVisibility(R.id.tv_one);
                holder.setViewVisibility(R.id.tv_two);
                holder.setViewGone(R.id.tv_three);
            } else if (splitTags.length == 3) {
                holder.setText(R.id.tv_one, splitTags[0]);
                holder.setText(R.id.tv_two, splitTags[1]);
                holder.setText(R.id.tv_three, splitTags[2]);
                holder.setViewVisibility(R.id.tv_one);
                holder.setViewVisibility(R.id.tv_two);
                holder.setViewVisibility(R.id.tv_three);
            }
        }

        if (data.getsParts() == 2) {
            holder.setText(R.id.tv_story_name, data.getStoryPackageTitle());
            holder.setText(R.id.tv_story_desc, data.getStoryPackageDetailTitle());
            ImgLoadUtil.displayStoryImage(mIVStory, data.getStoryPackageImage());
            holder.setText(R.id.tv_left, "共" + data.getStoryPackageCount() + "集");
            holder.getView(R.id.iv_enter_story_list).setVisibility(View.VISIBLE);
            holder.getView(R.id.iv_more).setVisibility(View.GONE);
            holder.setViewGone(R.id.tv_right);
            holder.setResImage(R.id.iv_left_mark, R.drawable.icon_package_count);
            holder.getView(R.id.down_story).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        } else {
            holder.setResImage(R.id.iv_left_mark, R.drawable.icon_listen_count);
            holder.setText(R.id.tv_story_name, data.getStoryTitle());
            holder.setText(R.id.tv_story_desc, data.getStoryDetailTitle());
            holder.setText(R.id.tv_left, data.getStoryListenCountString());
            holder.setText(R.id.tv_right, data.getStoryCreateTimeString());
            ImgLoadUtil.displayStoryImage(mIVStory, data.getStoryImage());
            holder.getView(R.id.iv_enter_story_list).setVisibility(View.GONE);
            holder.getView(R.id.iv_more).setVisibility(View.VISIBLE);
            holder.setViewVisibility(R.id.tv_right);
            data.setDownState(data.getDownState() == 1 ? 1 : DownHelper.isDown(mContext, data.getStoryId()) ? 1 : 0);


            holder.getView(R.id.down_story).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    storyListBean = data ;
                    showMoreWindow(holder.getView(R.id.iv_more) ,storyListBean);
                }
            });
        }

    }

    StoryListBean storyListBean ;

    private void showMoreWindow(View view ,StoryListBean data) {
         showPopup(view ,data);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_commend_story;
    }

    @Override
    protected void collect() {
         collect(storyListBean);
    }

    @Override
    protected void download() {
        downLoad(storyListBean);
    }

    @Override
    protected void share() {
        share(storyListBean);
    }
}
